####################################
#EU Member State Support to Ukraine#
####################################

# Clear working space
rm(list = ls())

# Set working directory
setwd("")

# Install packages
install.packages("QCA")
install.packages("cna")
install.packages("frscore")
install.packages("SetMethods")

# Load packages
library(QCA)
library(cna)
library(frscore)
library(SetMethods)

#LOAD RAW DATA
rawdata <- read.csv("SUPPORT.csv", sep = ",",dec = ".")
rawdata

#CALIBRATIONS AND FUZZY DATA

CASE <-rawdata$CASE
SUPPORT <- calibrate(rawdata$SUPPORT, logistic = FALSE, thresholds = "e=0.025, c = 0.25, i = 0.5")
THREAT  <- calibrate(rawdata$THREAT, logistic = FALSE, thresholds = "e=950, c =600,i = 0")
GDP <- calibrate(rawdata$GDP, logistic = FALSE, thresholds = "e=100, c = 550, i = 2500")
DEBT <- calibrate(rawdata$DEBT, logistic = FALSE, thresholds = "e=60, c = 40, i = 20")
DEF <- calibrate(rawdata$DEF, logistic = FALSE, thresholds = "e=-3, c = -1.5, i = 0")
BS <- fuzzyor(DEBT,DEF)
MILINV <- calibrate(rawdata$MILINV, logistic = FALSE, thresholds = "e=0.05, c = 0.2, i = 0.4")
RGOV  <- calibrate(rawdata$RGOV, logistic = FALSE, thresholds = "e=2.5, c = 5, i = 7.5")
PS <- calibrate(rawdata$PS, logistic = FALSE, thresholds = "e=40, c = 50, i = 60")
GDPCAP <- calibrate(rawdata$GDPCAP, logistic = FALSE, thresholds = "e=10000, c = 25000, i = 50000")

#Export fuzzy data

fuzzydata<- data.frame(CASE, MILINV, GDP, BS,THREAT,RGOV,PS,SUPPORT)
fuzzydata
write.table(fuzzydata, "fuzzy.csv", row.names= F, sep = ";", dec =".")
fuzzydata<- read.csv("fuzzy.csv", row.names = 1, sep = ";", dec = ".")

#Application frscore procedure CNA Presence Outcome (Results presented in Table 1 main text and Table A1.1 Appendix)

frscored_cna(fuzzydata,type = "fs",
             fit.range = c(1, 0.75),
             granularity = 0.05,
             output = c("asf"),
             scoretype = c("full", "supermodel", "submodel"),
             normalize = c("truemax"),
             verbose = FALSE,
             maxsols = 20,
             test.model = NULL,
             print.all = TRUE, outcome = "SUPPORT", maxstep = c(4,4,6))

#Check consistency and coverage solution CNA Presence Outcome (Results presented in Table 1 main text)

condition("THREAT + MILINV*PS-> SUPPORT", fuzzydata, show.cases=T)
condition("THREAT -> SUPPORT", fuzzydata, show.cases=T)
condition("MILINV*PS-> SUPPORT", fuzzydata, show.cases=T)

#Application frscore procedure CNA Absence Outcome (Results presented in Table 1 main text and and Table A1.2 Appendix)


frscored_cna(fuzzydata,type = "fs",
             fit.range = c(1, 0.75),
             granularity = 0.05,
             output = c("asf"),
             scoretype = c("full", "supermodel", "submodel"),
             normalize = c("truemax","idealmax","none"),
             verbose = FALSE,
             maxsols = 50,
             test.model = NULL,
             print.all = TRUE, outcome = "support", maxstep = c(4,4,6))


#Check consistency and coverage solution CNA Absence Outcome (Results presented in Table 1 main text)

condition("ps*threat + milinv*threat-> support", fuzzydata, show.cases=T)
condition("ps*threat -> support", fuzzydata, show.cases=T)
condition("milinv*threat-> support", fuzzydata, show.cases=T)

#Produce xy plots main text and appendix

#Prepare data to produce xy plots
SOLSUPPORT<- fuzzyor(THREAT,fuzzyand(MILINV,PS))
SOLMILINVPS <-fuzzyand(MILINV,PS)
SUPPORT0<-1-SUPPORT
PS0<-1-PS
MILINV0 <-1-MILINV
THREAT0<-1-THREAT
SOLSUPPORT0 <- fuzzyor(fuzzyand(PS0,THREAT0),fuzzyand(MILINV0,THREAT0))
SOLPSTHREAT <- fuzzyand(PS0,THREAT0)
SOLMILINVTHREAT <- fuzzyand(MILINV0,THREAT0)

soldata <-data.frame(CASE,SUPPORT,SOLSUPPORT,THREAT,SOLMILINVPS,SUPPORT0,SOLSUPPORT0,SOLPSTHREAT,SOLMILINVTHREAT)
soldata

write.table(soldata, "solutions.csv", row.names= F, sep = ";", dec =".")

solutions <- read.csv("solutions.csv", row.names = 1, sep = ";", dec = ".")
solutions

#Produce xy plots

#xy plot figure 1 main text

xy.plot("SOLSUPPORT", "SUPPORT", data=solutions, 
        labcol = "black",
        main = "XY plot SUPPORT", 
        ylab = "SUPPORT", 
        xlab = "SOLUTION",
        necessity = FALSE,
        jitter = TRUE, 
        font = "arial",
        fontface = "plain", 
        fontsize = 4,
        labs = rownames(solutions),
        crisp = FALSE,
        shape = 20,
        consH = FALSE)

#xy plot figure 2 main text

xy.plot("SOLSUPPORT0", "SUPPORT0", data=solutions, 
        labcol = "black",
        main = "XY plot ~SUPPORT", 
        ylab = "~SUPPORT", 
        xlab = "SOLUTION",
        necessity = FALSE,
        jitter = TRUE, 
        font = "arial",
        fontface = "plain", 
        fontsize = 4,
        labs = rownames(solutions),
        crisp = FALSE,
        shape = 20,
        consH = FALSE)

#xy plot figure A2.1 appendix

xy.plot("THREAT", "SUPPORT", data=solutions, 
        labcol = "black",
        main = "XY plot THREAT", 
        ylab = "SUPPORT", 
        xlab = "THREAT",
        necessity = FALSE,
        jitter = TRUE, 
        font = "arial",
        fontface = "plain", 
        fontsize = 4,
        labs = rownames(solutions),
        crisp = FALSE,
        shape = 20,
        consH = FALSE)

#xy plot figure A2.2 appendix

xy.plot("SOLMILINVPS", "SUPPORT", data=solutions, 
        labcol = "black",
        main = "XY plot MILINV*PS", 
        ylab = "SUPPORT", 
        xlab = "MILINV*PS",
        necessity = FALSE,
        jitter = TRUE, 
        font = "arial",
        fontface = "plain", 
        fontsize = 4,
        labs = rownames(solutions),
        crisp = FALSE,
        shape = 20,
        consH = FALSE)

#xy plot figure A2.3 appendix

xy.plot("SOLPSTHREAT", "SUPPORT0", data=solutions, 
        labcol = "black",
        main = "XY plot ~PS*~THREAT", 
        ylab = "~SUPPORT", 
        xlab = "~PS*~THREAT",
        necessity = FALSE,
        jitter = TRUE, 
        font = "arial",
        fontface = "plain", 
        fontsize = 4,
        labs = rownames(solutions),
        crisp = FALSE,
        shape = 20,
        consH = FALSE)

#xy plot figure A2.4 appendix

xy.plot("SOLMILINVTHREAT", "SUPPORT0", data=solutions, 
        labcol = "black",
        main = "XY plot ~MILINV*~THREAT", 
        ylab = "~SUPPORT", 
        xlab = "~MILINV*~THREAT",
        necessity = FALSE,
        jitter = TRUE, 
        font = "arial",
        fontface = "plain", 
        fontsize = 4,
        labs = rownames(solutions),
        crisp = FALSE,
        shape = 20,
        consH = FALSE)

#Prepare data for alternative analysis appendix

fuzzydataR<- data.frame(CASE, MILINV, GDPCAP, BS,THREAT,RGOV,PS,SUPPORT)
fuzzydataR
write.table(fuzzydataR, "fuzzy.csv", row.names= F, sep = ";", dec =".")
fuzzydataR<- read.csv("fuzzy.csv", row.names = 1, sep = ";", dec = ".")

#Application frscore procedure CNA Presence Outcome alternative analysis appendix (Results presented in Table A3.1 appendix)

frscored_cna(fuzzydataR,type = "fs",
             fit.range = c(1, 0.75),
             granularity = 0.05,
             output = c("asf"),
             scoretype = c("full", "supermodel", "submodel"),
             normalize = c("truemax"),
             verbose = FALSE,
             maxsols = 20,
             test.model = NULL,
             print.all = TRUE, outcome = "SUPPORT", maxstep = c(4,4,6))

#Application frscore procedure CNA Presence Outcome alternative analysis appendix (Results presented in Table A3.2 appendix)


frscored_cna(fuzzydataR,type = "fs",
             fit.range = c(1, 0.75),
             granularity = 0.05,
             output = c("asf"),
             scoretype = c("full", "supermodel", "submodel"),
             normalize = c("truemax","idealmax","none"),
             verbose = FALSE,
             maxsols = 50,
             test.model = NULL,
             print.all = TRUE, outcome = "support", maxstep = c(4,4,6))

  